package com.txsj.scan.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.txsj.scan.R;
import com.txsj.scan.bean.SimpleBatchBean;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Created by WangChuangJun on 2018/11/21
 * @Describe 绑定商品添加批次信息
 */
public class SimpleBatchAdapter extends RecyclerView.Adapter<SimpleBatchAdapter.BatchViewHolder> {
    private List<SimpleBatchBean> list;
    private Context context;
    private int lastPosition;//上一次选中的位置

    public SimpleBatchAdapter(Context context) {
        this.context = context;
        this.list = new ArrayList<>();
    }

    /**
     * 分页
     *
     * @param list
     * @param pageNo
     */
    public void setList(List<SimpleBatchBean> list, int pageNo) {
        if (pageNo <= 1) {
            this.list.clear();
            list.get(0).setSelect(true);
            lastPosition = 0;
        }
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    public void clear(){
        list.clear();
        notifyDataSetChanged();
    }

    @Override
    public BatchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new BatchViewHolder(LayoutInflater.from(context).inflate(R.layout.item_simple_batch, parent,
                false));
    }

    @Override
    public void onBindViewHolder(final BatchViewHolder holder, final int position) {
        final SimpleBatchBean batchBean = list.get(position);
        SimpleDateFormat simpleDateFormat0 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyyMMdd");
        try {
            Date date = simpleDateFormat0.parse(batchBean.getExpiry_time());
            Date date_birth = simpleDateFormat0.parse(batchBean.getCreation_time());

            holder.tv_end_time.setText(simpleDateFormat1.format(date));
            holder.tv_birth_date.setText(simpleDateFormat1.format(date_birth));
            holder.tv_kucun_num.setText("0");
            if (batchBean.isSelect()) {
                holder.box_check.setBackgroundResource(R.drawable.box_select_s);
            } else {
                holder.box_check.setBackgroundResource(R.drawable.box_select_n);
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != listener){
                        if (batchBean.isSelect())return;
                        batchBean.setSelect(true);
                        list.get(lastPosition).setSelect(false);
                        lastPosition = position;
                        listener.onClick(position);
                    }
                }
            });
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public SimpleBatchBean get(int position) {
        return list.get(position);
    }

    public class BatchViewHolder extends RecyclerView.ViewHolder {
        TextView tv_end_time, tv_birth_date, tv_kucun_num,tv_batch_num;
        ImageView iv_decrease, iv_increase, iv_del,box_check;
        TextView et_goods_num;

        public BatchViewHolder(View itemView) {
            super(itemView);
            tv_end_time = (TextView) itemView.findViewById(R.id.tv_end_time);
            tv_birth_date = (TextView) itemView.findViewById(R.id.tv_birth_date);
            tv_kucun_num = (TextView) itemView.findViewById(R.id.tv_kucun_num);
            tv_batch_num = (TextView) itemView.findViewById(R.id.tv_batch_num);
            iv_decrease = (ImageView) itemView.findViewById(R.id.iv_decrease);
            iv_increase = (ImageView) itemView.findViewById(R.id.iv_increase);
            et_goods_num = (TextView) itemView.findViewById(R.id.et_goods_num);
            iv_del = (ImageView) itemView.findViewById(R.id.iv_del);
            box_check = (ImageView) itemView.findViewById(R.id.box_check);
        }
    }


    private OnItemClickListener listener;
    public void setListener(OnItemClickListener listener) {
        this.listener = listener;//EditText改变
    }


    public interface OnItemClickListener {
        void onClick(int position);
    }
}
